/******************************************************************************
 *	@Author Raffaello Brondi
 *
 * 	@description Main FB file
 *
 ******************************************************************************/

/* List of fb events */
var _fb_events_list = {
	"login" 		: 'auth.login', //fired when the auth status changes to connected
	"authResponseChange" 	: 'auth.authResponseChange' , //fired when the authResponse changes
	"statusChange" 		: 'auth.statusChange' , //fired when the status changes (see FB.getLoginStatus for additional information on what this means)
	"logout"		: 'auth.logout', //fired when the user logs out.
	"prompt"		:'auth.prompt', //fired when user is prompted to log in or opt in to Platform after clicking a Like button.

	"render" 		: 'xfbml.render',//fired when a call to FB.XFBML.parse() completes.

	"edge.create" 		: 'edge.create', //fired when the user likes something (fb:like). The response parameter to the callback function contains the URL that was liked
	"edge.remove" 		: 'edge.remove', //fired when the user unlikes something (fb:like). The response parameter to the callback function contains the URL that was unliked:

	"comment.create"	: 'comment.create', //fired when the user adds a comment (fb:comments). The response object passed into the callback function looks like:
	"comment.remove"	: 'comment.remove', //fired when the user removes a comment (fb:comments). The response object passed into the callback function looks like:

	"message.send" 		: 'message.send' //fired when the user sends a message using the send button. The response object passed into the callback function contains the URL which was sent:
};

var _fb_user = new User();

/**
 * Get FB API asyncronously
 */
function initFB(){
	FB.init({
		appId : '129522767159878',
		status : true, // check login status
		cookie : true, // enable cookies to allow the server to access the session
		xfbml : true, // parse XFBML
		channelUrl : 'https://www.sonna.unisi.it/admin/fb_channel.html', // Custom Channel URL
		oauth : true //enables OAuth 2.0
	});

	subscribe2Event(_fb_events_list['authResponseChange'], statusChange);
	subscribe2Event(_fb_events_list['comment.create'], saveComment);
	subscribe2Event(_fb_events_list['edge.create'], _fb_user.like);
	subscribe2Event(_fb_events_list['edge.remove'], _fb_user.unlike);

	_fb_user.addListener('onProfileAcquired', showUser);
};


function statusChange(response){
//	log("FB status change to " + response.status);
	if(response == undefined || response.status == undefined || response.status == "unknown"){
		_fb_user.logout();
		hideLogoutBtn();
		hideUser();
		showLoginBtn();
		return;
	}

	if(response.status == "connected"){
		_fb_user.login(response.authResponse);
		hideLoginBtn();
		showLogoutBtn();
	}
}

function postProfileMessage(message, onSuccess, onFailure) {
	FB.api('/me/feed', 'post', {
		message : message,
	}, function(response) {
		if(!response || response.error) {
			//log('postProfileMessage:Error occured');
			onFailure(response);
		} else {
			//alert('Post ID: ' + response.id);
			onSuccess(response);
		}
	});
}

function saveComment(obj) {
	var commentID = obj.commentID
		var href = obj.href;
	//alert(commentID);


	FB.api(commentID, 'get', {}, function(response) {
		if(!response)
		alert('bug facebook non ancora corretto, vedere https://developers.facebook.com/bugs/231111006992900?browse=search_4fd9b10abcb673283298676');
		else
		alert('bug corretto! ' + response);
	});
}

/*******************************************************************************
 * @description Binds the callback function to the facebook event.
 * @param callback Callback function called whenever the event is fired
 * @param evt The FB event to subscribe to 
 ******************************************************************************/
function subscribe2Event(evt, callback) {
	FB.Event.subscribe(evt, callback);
}

function hideLoginBtn(){
	jQuery('#fb_login').hide('fast');
}

function showLoginBtn(){
	if(jQuery('#fb_login').is(":visible"))
		return;
	jQuery('#fb_login').show('fast');
}

function hideLogoutBtn(){
	jQuery('#fb_logout').hide('fast');
}

function showLogoutBtn(){
	if(jQuery('#fb_logout').is(":visible"))
		return;

	var spinner= jQuery('<div></div>');

	spinner.append('<span>Loading profile information</span>');
	spinner.append('<img src="' + Drupal.settings.sonna_login.images.loading + '"/>');

	jQuery('#fb_loading').prepend(spinner);	
	jQuery('#fb_logout').show('fast');
}

function showUser(){
	if(jQuery('#fbUserDetails').length > 0)
		return;
	
	var user = jQuery('<div id="fbUserDetails"></div>');

	user.append('<img src="http://graph.facebook.com/' + _fb_user.userId + '/picture"/>');
	user.append('<p>Sei attualmente connesso a Facebook come <b>' + _fb_user.getName() + '</b></p>');
	user.append('<p>Se non sei tu effettua il logout</p>');

	jQuery('#fb_user').prepend(user);	
	jQuery('#fb_loading').empty();	
}

function hideUser(){
	jQuery('#fb_user').empty();	
}
